//
//  DetailInfoViewController.m
//  HengHuaSupervision
//
//  Created by wanglong8889@126.com on 14-3-21.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "DetailInfoViewController.h"
#import "XDHeader.h"
#import "XDTools.h"
#import "DetailInfoCell.h"
#import <QuartzCore/QuartzCore.h>
#import "PropertyItem.h"

@interface DetailInfoViewController ()

@end

@implementation DetailInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataArray = [[NSMutableArray alloc] init];
    [self initDetailInfoViews];
    
	// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self calculateRemindBtnFrame:[XDTools sharedXDTools].waitCumitCount];
    [self calculateRemindBtnFrame:[[NSUserDefaults standardUserDefaults] valueForKey:@"waitCunitCount"]];
    [self initDatas];
}
-(void)initDatas
{
    [_dataArray removeAllObjects];
    FMDatabase * db = [XDTools getDb];
    if (![db open]){
        DDLOG(@"数据库打开失败");
        return;
    }
    FMResultSet * rs = [db executeQuery:@"SELECT * FROM T_TRACE_ASSERTS WHERE id = ?",_assertId];
    while ([rs next]) {
        PropertyItem * item = [[PropertyItem alloc] init];
        item.code = [rs objectForColumnName:@"code"];
        item.name = [rs objectForColumnName:@"name"];
        item.model = [rs objectForColumnName:@"model"];
        item.type = [rs objectForColumnName:@"type"];
        
        item.factory_name = [rs objectForColumnName:@"factory_name"];
        item.factory_code = [rs objectForColumnName:@"factory_code"];
        item.factory_phone = [rs objectForColumnName:@"factory_phone"];
        item.factory_linkman = [rs objectForColumnName:@"factory_linkman"];
        item.factory_addr = [rs objectForColumnName:@"factory_addr"];
        
        item.supervision_unitname = [rs objectForColumnName:@"supervision_unitname"];
        item.supervision_unitcode = [rs objectForColumnName:@"supervision_unitcode"];
        item.supervision_mode = [rs objectForColumnName:@"supervision_mode"];
        item.supervision_linkman = [rs objectForColumnName:@"supervision_linkman"];
        
        [_dataArray addObject:item];
    }
    [db close];
    
    FMDatabase * db2 = [XDTools getDb];
    if (![db2 open]){
        DDLOG(@"备注-->数据库打开失败");
        return;
    }
    FMResultSet * rs2 = [db2 executeQuery:@"SELECT * FROM T_TRACE_SUPERVISION WHERE assert_id = ?",_assertId];
    while ([rs2 next]) {
        markString = [rs2 objectForColumnName:@"notes"];
    }
    [db2 close];
    
    
    [detailTableView reloadData];
}

#pragma mark UI create
-(void)initDetailInfoViews{
    UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 46)];
    topView.image = [[UIImage imageNamed:@"detail_info-label"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, .3, 10, .3)];
    

    UILabel *detailLabel = [UILabel initLabel:CGRectMake(21, 0, 100, mLabelEdit_Head_Img.size.height) textFont:mHomepage_text_Font textColor:mLabelEdit_Title_Color textAlignment:NSTextAlignmentLeft];
    detailLabel.text = @"详细信息";
    [topView addSubview:detailLabel];
    [self.contentView addSubview:topView];
    
    detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, topView.frame.origin.y+topView.frame.size.height+15, self.contentView.frame.size.width-10-85, 600) style:UITableViewStylePlain];
    detailTableView.delegate = self;
    detailTableView.dataSource = self;
    detailTableView.rowHeight = 56.0;
    detailTableView.scrollEnabled = NO;
    detailTableView.backgroundColor = [UIColor clearColor];
    detailTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.contentView addSubview:detailTableView];
    
}
#pragma mark  UitableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section+2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifer = @"CellDetail";
    DetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[DetailInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([_dataArray count]==0){
        return cell;
    }
    PropertyItem * item = [_dataArray objectAtIndex:0];
    if (indexPath.section ==0){
        cell.labelFirst.text = [[self.details_info_array objectAtIndex:indexPath.row] objectAtIndex:0];
        cell.labelThird.text = [[self.details_info_array objectAtIndex:indexPath.row] objectAtIndex:1];
        if (indexPath.row == 0){
            cell.labelSec.text = item.code;
            cell.labelFour.text = item.name;
        }else{
            cell.labelSec.text = item.model;
            cell.labelFour.text = item.type;
        }
    }else if (indexPath.section == 1){
        cell.labelFirst.text = [[self.details_info_array objectAtIndex:indexPath.row+2]objectAtIndex:0];
        cell.labelThird.text = [[self.details_info_array objectAtIndex:indexPath.row +2] objectAtIndex:1];
        if (indexPath.row==0){
            cell.labelSec.text = item.factory_name;
            cell.labelFour.text = item.factory_code;
        }else if (indexPath.row == 1){
            cell.labelSec.text = item.factory_phone;
            cell.labelFour.text = item.factory_linkman;
        }else if (indexPath.row ==2){
            cell.labelSec.text =nil;
            cell.labelFour.text =nil;
        }
    }else if (indexPath.section == 2){
        cell.labelFirst.text = [[self.details_info_array objectAtIndex:indexPath.row+5] objectAtIndex:0];
        cell.labelThird.text = [[self.details_info_array objectAtIndex:indexPath.row+5] objectAtIndex:1];
        if (indexPath.row == 0){
            cell.labelSec.text =item.supervision_unitname;
            cell.labelFour.text =item.supervision_unitcode;
        }else if (indexPath.row ==1){
            cell.labelSec.text =item.supervision_mode;
            cell.labelFour.text =item.supervision_linkman;
        }else if (indexPath.row ==2){
            cell.labelSec.text =@"已出厂";
            cell.labelFour.text =nil;
        }else{
            cell.labelSec.text =nil;
            cell.labelFour.text =nil;
        }
    
    }
    
    UILabel *addressLabel = [UILabel initLabel:CGRectMake(cell.labelFirst.frame.origin.x+cell.labelFirst.frame.size.width+16, 0, 785-28, 56) textFont:[UIFont systemFontOfSize:15.0] textColor:RGBA(43, 43, 43) textAlignment:NSTextAlignmentCenter];
    addressLabel.backgroundColor = [UIColor whiteColor];
    addressLabel.text = item.factory_addr;
    
    UILabel *BeizhuLabel = [UILabel initLabel:CGRectMake(cell.labelFirst.frame.origin.x+cell.labelFirst.frame.size.width, 0, 785+6, 56) textFont:[UIFont systemFontOfSize:15.0] textColor:RGBA(43, 43, 43) textAlignment:NSTextAlignmentCenter];
    BeizhuLabel.backgroundColor = [UIColor clearColor];
    
    UIImageView * imag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 785+6, 55)];
    imag.image = [XDTools stretchableImag:mHomepage_Cell_Even_Img];
    [BeizhuLabel addSubview:imag];
    
    UITextField * beizhuTf = [[UITextField alloc] initWithFrame:CGRectMake(5,1, 440+590/2.0f, 27)];
    beizhuTf.text = markString;
    beizhuTf.textAlignment = NSTextAlignmentCenter;
    beizhuTf.font = [UIFont systemFontOfSize:15.0];
    beizhuTf.textColor = RGBA(43, 43, 43);
    beizhuTf.userInteractionEnabled = NO;
    beizhuTf.backgroundColor = [UIColor clearColor];
    UIImageView *tfView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.labelFirst.frame.origin.x+cell.labelFirst.frame.size.width+1+22, 13, 448, 29)];
    tfView.userInteractionEnabled = YES;
    tfView.image = [[UIImage imageNamed:@"textField_detail_xy"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, .5, 10, .5)];
    tfView.backgroundColor = [UIColor clearColor];
    [tfView addSubview:beizhuTf];
    
    
    if (indexPath.section == 1&&indexPath.row == 2) {
        [cell addSubview:addressLabel];
    }
    if (indexPath.section == 2&&indexPath.row == 3) {
        [cell addSubview:BeizhuLabel];
        [cell addSubview:tfView];
    }
    if (indexPath.row%2 == 0){
        cell.diselectBg.image = [[UIImage imageNamed:@"cell_detail_lan"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, .5, 10, .3)];
    }else{
        cell.diselectBg.image = [[UIImage imageNamed:@"cell_detail_white"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, .5, 10, .3)];
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 32;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *sectionFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  self.contentView.frame.size.width-10-85, 32)];
    sectionFooterView.backgroundColor = [UIColor clearColor];
    return sectionFooterView;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
